function Add_New_Export_Line(Edit) {

window.openDialog("chrome://Ultima/content/Ultima_Export_Line_Window.xul" , "Export Line" , "chrome,titlebar,toolbar,alwaysraised,minimizable,centerscreen,resizable",Edit).focus();

}


function Get_Export_Line( Name ) {

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var Export_Line_List = prefService.getCharPref('extensions.Ultima.Export_Line_List').split('|');

for(q=0; q<Export_Line_List.length; q++) {
var entry = Export_Line_List[q].split('#');
if ( Name == entry[0] ) return entry;
}

return false;

}


function Edit_Export_Line() {

var Tree_Export_Line = document.getElementById( 'Export_Line_Tree' );
var Name = Tree_Export_Line.view.getCellText( Tree_Export_Line.currentIndex , Tree_Export_Line.columns.getNamedColumn( 'Head_Name' ) );

Add_New_Export_Line( Get_Export_Line( Name ) );

}


function Populate_Export_Lines() {

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var Export_Line_List = prefService.getCharPref( 'extensions.Ultima.Export_Line_List' ).split('|');

var temp = "";

for (q=1; q<Export_Line_List.length; q++) {
temp = Export_Line_List[q].split('#');
Create_Export_Line(temp[0],temp[1]);
}

}


function Clear_Export_Lines() {

var Tree_Children_Export_Line = document.getElementById( 'Tree_Children_Export_Line' );

if (Tree_Children_Export_Line) {

var Item = Tree_Children_Export_Line.getElementsByTagName( 'treeitem' );

if (Item && Item.length > 0) for(q=Item.length-1; q>=0; q--) Tree_Children_Export_Line.removeChild( Item[q] );

}

}


function Create_Export_Line( Name , Data ) {

var new_item = "";
var new_cell = "";
var new_rows = "";

new_item = document.createElement('treeitem'); new_item.setAttribute( 'id'    , Name );
new_cell = document.createElement('treecell'); new_cell.setAttribute( 'label' , Name );
new_rows = document.createElement('treerow' ); new_rows.appendChild( new_cell );
new_cell = document.createElement('treecell'); new_cell.setAttribute( 'label' , Data );

new_rows.appendChild(new_cell);
new_item.appendChild(new_rows);

var Tree_Children_Export_Line = document.getElementById( 'Tree_Children_Export_Line' );
if (Tree_Children_Export_Line) Tree_Children_Export_Line.appendChild( new_item );

}


function Update_Buttons() {

var selected = 0;

var start = new Object();
var end = new Object();

var Current_Tree = document.getElementById("Export_Line_Tree");
var Num_Ranges = Current_Tree.view.selection.getRangeCount();

for (var tt = 0; tt < Num_Ranges && selected < 2; tt++) {
Current_Tree.view.selection.getRangeAt(tt,start,end);
for (var itemIndex = start.value; itemIndex <= end.value && selected < 2; itemIndex++) selected++;
}

if (selected == 1) {

document.getElementById( "Export_Line_Edit_Button"   ).disabled = false;
document.getElementById( "Export_Line_Remove_Button" ).disabled = false;
document.getElementById( "Export_Line_Run_Button"    ).disabled = false;
document.getElementById( "Export_Line_Copy_Button"   ).disabled = false;	

} else {

document.getElementById( "Export_Line_Edit_Button"   ).disabled = true;
document.getElementById( "Export_Line_Remove_Button" ).disabled = true;
document.getElementById( "Export_Line_Run_Button"    ).disabled = true;
document.getElementById( "Export_Line_Copy_Button"   ).disabled = true;

}

}


function Remove_Export_Line() {

var start = new Object();
var end = new Object();

var Current_Tree = document.getElementById( "Export_Line_Tree" );
var Num_Ranges = Current_Tree.view.selection.getRangeCount();

for (var ww = Num_Ranges - 1; ww >= 0; ww--) {

Current_Tree.view.selection.getRangeAt(ww,start,end);

for (var qq = end.value; qq >= start.value; qq--) {

item = Current_Tree.view.getItemAtIndex(qq);
item.parentNode.removeChild(item);

Name = item.firstChild.firstChild.getAttribute( "label" );
Remove_Export_Line_Entry( Name );

}

}

}


function Remove_Export_Line_Entry( Name ) {

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var Export_Line_List = prefService.getCharPref( 'extensions.Ultima.Export_Line_List' ).split('|');

for(q=0; q<Export_Line_List.length; q++) {

var entry = Export_Line_List[q].split('#');

if ( Name == entry[0] ) {

Export_Line_List.splice(q,1);
Temp_Entry = Export_Line_List.join('|');
prefService.setCharPref( 'extensions.Ultima.Export_Line_List' , Temp_Entry );

}

}

Clear_Export_Lines();
Populate_Export_Lines();

}


function Remove_All_Export_Line() {

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
prefService.setCharPref( 'extensions.Ultima.Export_Line_List' , "" );

document.getElementById( "Export_Line_Remove_All_Button" ).disabled = true ;

Clear_Export_Lines();
Populate_Export_Lines();

}

















function Copy_Export_Line_To_Clipboard(copytext) {

var str = null;
var trans = null;
var clip = null;
var clipid = null;

str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString); 
if (!str) return false; 
 
str.data = copytext; 
 
trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable); 
if (!trans) return false; 
 
trans.addDataFlavor("text/unicode"); 
trans.setTransferData("text/unicode", str, copytext.length * 2); 
 
clipid = Components.interfaces.nsIClipboard; 
clip = Components.classes["@mozilla.org/widget/clipboard;1"].getService(clipid); 
if (!clip) return false; 
 
clip.setData(trans, null, clipid.kGlobalClipboard); 
copytext = null;
str = null;
trans = null;
clip = null;
clipid = null
return false;

}


function Copy_Export_Line() {

var Tree_Export_Line = document.getElementById( 'Export_Line_Tree' );
var Export_Line_Data = Tree_Export_Line.view.getCellText( Tree_Export_Line.currentIndex , Tree_Export_Line.columns.getNamedColumn( 'Head_Data' ) );

Copy_Export_Line_To_Clipboard( Export_Line_Data );

}


function Run_Export_Line() {

var Export_Line_Data = "";
var Tree_Export_Line = document.getElementById( 'Export_Line_Tree' );
var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);

Export_Line_Data = Tree_Export_Line.view.getCellText( Tree_Export_Line.currentIndex , Tree_Export_Line.columns.getNamedColumn( 'Head_Data' ) );

if ( Export_Line_Data != "" ) prefService.setCharPref( "extensions.Ultima.ClipboardExportLine" , Export_Line_Data );
if ( Export_Line_Data != "" ) window.open("chrome://Ultima/content/Clipboard.xul" ,"Clipboard Manager" ,"width=500,height=600,chrome=yes,centerscreen=yes,menubar=no,toolbar=no,location=no,personalbar=no,status=no,resizable=yes,scrollbars=no").focus();

}
